<?php
namespace Topxia\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Topxia\Common\Paginator;
use Topxia\Common\ArrayToolkit;

class ZhiboController extends BaseController
{
		function __construct(){
			//    parent::__construct();
			require_once(__dir__."../../../Common/Pili_v2.php");
	    }
    public function indexAction(Request $request)
    {
    	$category=$this->getCategoryService()->findAllCategories();
		$newcategory = array();
		foreach($category as $k => $v){
			$newcategory[$v['id']] = $v['name'];
		}
      return $this->render('TopxiaAdminBundle:Zhibo:index.html.twig', array(
      	'courses'=>array(),
      	'category'=>$newcategory
	  ));
    }
	public function getzhiboliuAction(Request $request)
    {
    	$ak = "F7goTUwYz-YtkPhD2TClGRWn3DFCFf3dCUBkbvma";
		$sk = "sPzX4qamBL9uszCIO4nPkH67u4oDyq1yIWRiofYU";
		$hubName = "10371vip";
		//创建hub
		$mac = new \Qiniu\Pili\Mac($ak, $sk);

		$client = new \Qiniu\Pili\Client($mac);

		$hub = $client->hub($hubName);
		$resp = $hub->listStreams("", 100, "");
		return $this->createJsonResponse($resp['keys']);
	}	
	public function getzhiboliu_urlAction(Request $request){
		$ak = "F7goTUwYz-YtkPhD2TClGRWn3DFCFf3dCUBkbvma";
		$sk = "sPzX4qamBL9uszCIO4nPkH67u4oDyq1yIWRiofYU";
		$hubName = "10371vip";
		//获取推流地址
		$streamKey=$_POST['name'];
		$url = \Qiniu\Pili\HLSPlayURL("pili-live-hdl.www.10371.vip", $hubName, $streamKey);
		return $this->createJsonResponse($url);
	}
	/*
	 * 查找教师
	 */
	public function getteacherAction(Request $request){
		$teracher=$this->getUserService()->getteacher($_POST['teacher'],"|ROLE_TEACHER|");
		return $this->createJsonResponse($teracher);
	}
	/*
	 * 保存直播
	 */
	public function set_zhiboAction(Request $request){
		$data=$_POST;
		$re=$this->getZhiboService()->setzhibo($data);
		var_dump($re);die;
	}
	/*
	 * 编辑直播
	 */
	public function editAction(Request $request, $id)
    {
        $user = $this->getUserService()->getUser($id);
        $zhibo = $this->getZhiboService()->get_zhibo_s($id);
        if ($request->getMethod() == 'POST') {
            $profile = $request->request->all();
              var_dump($request->getMethod());exit;
            return $this->redirect($this->generateUrl('admin_user'));
        }
        $category=$this->getCategoryService()->findAllCategories();
		$newcategory = array();
		foreach($category as $k => $v){
			$newcategory[$v['id']] = $v['name'];
		}
		$zhibo['zbtime']=date('Y-m-d H:i:s',$zhibo['zbtime']);
        return $this->render('TopxiaAdminBundle:Zhibo:exit.html.twig', array(
            'user' => $user,
            'zhibo'=>$zhibo,
            'category'=>$newcategory
        ));
    }
	/*
	 * 编辑保存直播
	 */
	public function esaveAction(Request $request,$id){
		$data=$_POST;
		$re=$this->getZhiboService()->esavezhibo($id,$data);
		return $this->createJsonResponse(array('message'=>'修改成功'));
	}
    protected function getUserService()
    {
        return $this->getServiceKernel()->createService('User.UserService');
    }
    protected function getCourseService()
    {
        return $this->getServiceKernel()->createService('Course.CourseService');
    }

    protected function getCourseDeleteService()
    {
        return $this->getServiceKernel()->createService('Course.CourseDeleteService');
    }

    protected function getCourseCopyService()
    {
        return $this->getServiceKernel()->createService('Course.CourseCopyService');
    }

    protected function getCategoryService()
    {
        return $this->getServiceKernel()->createService('Taxonomy.CategoryService');
    }

    protected function getTestpaperService()
    {
        return $this->getServiceKernel()->createService('Testpaper.TestpaperService');
    }

    protected function getAppService()
    {
        return $this->getServiceKernel()->createService('CloudPlatform.AppService');
    }

    protected function getClassroomService()
    {
        return $this->getServiceKernel()->createService('Classroom:Classroom.ClassroomService');
    }

    protected function getPasswordEncoder()
    {
        return new MessageDigestPasswordEncoder('sha256');
    }
	
	protected function getZhiboService()
    {
        return $this->getServiceKernel()->createService('Zhibo.ZhiboService');
    }

}
